<?php


namespace app\admin\controller\goods;

use app\admin\service\GoodsLayoutService;
use app\common\controller\BaseAdmin;
use app\common\model\GoodsLayout as GoodsLayoutModel;
use think\facade\Config;
use think\facade\Db;

class GoodsLayout extends BaseAdmin
{


    public function index(){
        $goods_id =$this->request->get('goods_id'); //获取楼盘id

        if(!is_numeric($goods_id)){
            $this->error('非法请求');
        }

        if ($this->request->isAjax()){
            //获取搜索条件
            $data =$this->request->except(['page'],'get');

            //管理员id
            $auth = Config::get('login_admin.auth_uid');
            $admin_id = session($auth); //获取当前登录管理员id

            if($admin_id != 1){
                $data['wxapp_id'] =$this->wxappid;
            }
            //end
            $data['goods_id'] =$goods_id;
            $model = new GoodsLayoutModel();
            $list = $model->withSearch($this->searchFields($data),$this->searchData($data))
                ->order('id','desc')
                ->paginate($data['limit'], false);
            $data =  $this->getTreeOrder($list->getCollection());
            return $this->tableJson($data,$list->total());
        }
        $this->assign('goods_id',$goods_id);
        //
        return $this->fetch('goods/layout/index');
    }

    /**
     * 新建/编辑模板
     * @return array|string
     */
    public function edit(){
        if($this->request->isGet()){
            $goods_id =$this->request->get('goods_id'); //获取楼盘id
            if(!is_numeric($goods_id)){
                $this->error('非法请求');
            }
        }


        if ($this->request->isAjax()){
            $data = $this->request->param();
            //编辑或者添加数据
            $scene = empty($data['id'])?'add':'edit';
            $data['wxapp_id'] =$this->wxappid;

          //  print_r($data);exit;
            return json(GoodsLayoutService::storeGoodsLayout($data,$scene));
        }
        //上级分类
        $parent = GoodsLayoutModel::where(['state'=>1])->field('id,name,goods_id')->select()->toArray();
      //  $parent =  $this->getTreeOrder($parent);
      //  print_r($parent);exit;
        $this->assign('cate', $parent);
        $id = $this->request->get('id');
        if ($id){
            $list = GoodsLayoutModel::where('id', $id)->find();

            $this->assign('list', $list);
        }

        $this->assign('goods_id',$goods_id);

        return $this->fetch('goods/layout/edit');
    }
    /**
     * 状态更改
     */
    public function change(){
        $id = $this->request->param('id');
        $model = GoodsLayoutModel::find($id);
        $model->state = $this->request->param('state');
        if ($model->save()){
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 删除批量删除
     */
    public function remove(){
        if ($this->request->isAjax()){
            $ids = $this->request->param('ids');

            GoodsLayoutModel::destroy($ids);

            //连带删除
            if (!is_array($ids)){
                $uid[] = $ids;
            }else{
                $uid = $ids;
            }

            GoodsLayoutModel::where('parent_id','in',$uid)->delete();
            $this->success('删除成功');

        }else{
            $this->error('非法请求');
        }
    }

}